﻿using Aspfm.Dal;
using Aspfm.Main;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace Aspfm.FtpFile
{
    public class AspfmFileRead
    {
        public static string TempPath = "temp.aspfm.file";

        public static WebResult GetFileId(string filepkid,string pathname = null)
        {
            if (!string.IsNullOrEmpty(filepkid))
            {
                IDbHelper idb = DbFactory.GetDB();
                return GetFile(filepkid, idb, pathname);
            }
            return new WebResult("not_find_file");
        }
        public static WebResult GetFile(string filepkid, IDbHelper idb = null, string pathname = null)
        {
            if (!string.IsNullOrEmpty(filepkid))
            {
                if (idb == null)
                {
                    idb = DbFactory.GetDB();
                }
                DataSet dsfile = idb.QuerySql("select * from Aspfm_FILES_Full where FILEPKID=st(0)", filepkid);
                if (DbFactory.CheckDSNotNull(dsfile))
                {
                    if (dsfile.Tables[0].Rows.Count > 0)
                    {
                        DateTime LASTWRITER = (DateTime)dsfile.Tables[0].Rows[0]["LASTWRITER"];
                        string ftppath = dsfile.Tables[0].Rows[0]["ftppath"].ToString();
                        string filepath = dsfile.Tables[0].Rows[0]["filepath"].ToString();
                        string filename = dsfile.Tables[0].Rows[0]["filename"].ToString();
                        string ispwd = dsfile.Tables[0].Rows[0]["ispwd"].ToString();
                        string guidpath;
                        string localfile = GetLocalFile(filepkid, filename, LASTWRITER, out guidpath);

                        if (!System.IO.File.Exists(localfile))
                        {
                            if (System.IO.File.Exists(ftppath + "/" + filepath))
                            {
                                if (ispwd == "0")
                                {
                                    System.IO.File.Copy(ftppath + "/" + filepath, localfile, true);
                                }
                            }
                            else
                            {
                                string ftpip = dsfile.Tables[0].Rows[0]["ftpip"].ToString();
                                string ftpport = dsfile.Tables[0].Rows[0]["ftpport"].ToString();
                                string ftpuser = dsfile.Tables[0].Rows[0]["ftpuser"].ToString();
                                string ftppwd = dsfile.Tables[0].Rows[0]["ftppwd"].ToString();

                                if (ispwd == "0")
                                {
                                    string errmsg = AspfmFtp.Download(localfile,
                                       filepath, ftpip, ftpport, ftpuser, ftppwd);
                                    if (!string.IsNullOrEmpty(errmsg))
                                    {
                                        return new WebResult(filepkid + "  " + errmsg);
                                    }
                                }
                            }
                        }
                        if (System.IO.File.Exists(localfile))
                        {
                            WebResult bresult = new WebResult();
                            bresult.Result.SetPropertyValue("filename",TempPath + "\\" + guidpath + "\\" + filename);
                            return bresult;
                        }
                    }
                }
            }
            return new WebResult("not_find_file");
        }

        public static List<WebResult> GetFileList(IDbHelper idb = null, params string[] filepkids)
        {
            List<WebResult> listfile = new List<WebResult>();
            if (filepkids != null && filepkids.Length > 0)
            {
                if (idb == null)
                {
                    idb = DbFactory.GetDB();
                }
                string allfilepath = null;
                foreach (string filepkid in filepkids)
                {
                    if (!string.IsNullOrWhiteSpace(filepkid))
                    {
                        if (allfilepath == null)
                        {
                            allfilepath = filepkid;
                        }
                        listfile.Add(GetFile(filepkid, idb, allfilepath));
                    }
                    else
                    {
                        listfile.Add(new WebResult(filepkid + "   not_find_file"));
                    }
                }
            }
            else
            {
                listfile.Add(new WebResult("not_find_file"));
            }
            return listfile;
        }

        private static string GetLocalFile(string filepkid, string filename,DateTime LASTWRITER, out string guidpath)
        {
            guidpath = filepkid;//Guid.NewGuid().ToString();//防止重复下载
            string localfile = DbFactory.CurrentPath().Trim('\\') + "\\" + TempPath + "\\" + guidpath;
            if (Directory.Exists(localfile) == false)
            {
                Directory.CreateDirectory(localfile);
            }
            return localfile + "\\" + filename;
        }
    }
}
